package com.zu.commu.auto1_0.protocol;

import com.zu.commu.exception.CommuException;

public class CheckData
{
  public byte getCrcCheck(byte[] buffer, int startIndex, int endIndex)
    throws Exception
  {
    byte tmp_data = 0;
    try {
      for (int i = startIndex; i <= endIndex; i++)
        tmp_data = (byte)(tmp_data + buffer[i]);
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new CommuException().setMessage("Auto1_0 计算效验和失败,数据不完整！", e);
    }

    return tmp_data;
  }

  public boolean checkCrc(byte[] b, int startIndex, int endIndex, int index)
    throws Exception
  {
    byte sum = getCrcCheck(b, startIndex, endIndex);
    if (sum == b[index]) {
      return true;
    }
    throw new CommuException().setMessage("效验和验证失败,放弃无效数据！", null);
  }
}

